﻿// -----------------------------------------------------------------------
// <copyright file="Animation.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DiddlySquat
{
	/// <summary>
	/// Class to display animations
	/// </summary>
	public class Animation
	{
		// The image representing the collection of images used for animation
		Texture2D spriteStrip;

		// The scale used to display the sprite strip
		float scale;

		// The time since we last updated the frame
		int elapsedTime;

		// The time we display a frame until the next one
		int frameTime;

		// The number of frames that the animation contains
		int frameCount;

		// The index of the current frame we are displaying
		int currentFrame;

		// The color of the frame we will be displaying
		Color color;

		// The area of the image strip we want to display
		Rectangle sourceRect;

		// The area where we want to display the image strip in the game
		Rectangle destinationRect;

		// Width of a given frame
		public int FrameWidth { get; private set; }

		// Height of a given frame
		public int FrameHeight { get; private set; }

		// The state of the Animation
		public bool Active { get; private set; }

		// Determines if the animation will keep playing or deactivate after one run
		public bool Looping { get; private set; }

		// Width of a given frame
		public Vector2 Position { get; set; }

		/// <summary>
		/// Initializes the new animation.
		/// </summary>
		/// <param name="texture">The texture.</param>
		/// <param name="position">The position.</param>
		/// <param name="frameWidth">Width of the frame.</param>
		/// <param name="frameHeight">Height of the frame.</param>
		/// <param name="frameCount">The frame count.</param>
		/// <param name="frametime">The frametime.</param>
		/// <param name="color">The color.</param>
		/// <param name="scale">The scale.</param>
		/// <param name="looping">if set to <c>true</c> [looping].</param>
		public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frametime, Color color, float scale, bool looping)
		{
			// Keep a local copy of the values passed in
			this.color = color;
			this.FrameWidth = frameWidth;
			this.FrameHeight = frameHeight;
			this.frameCount = frameCount;
			this.frameTime = frametime;
			this.scale = scale;

			Looping = looping;
			Position = position;
			spriteStrip = texture;

			// Set the time to zero
			elapsedTime = 0;
			currentFrame = 0;

			// Set the Animation to active by default
			Active = true;
		}

		/// <summary>
		/// Updates the animation.
		/// </summary>
		/// <param name="gameTime">The game time.</param>
		public void Update(GameTime gameTime)
		{
			// Do not update the game if we are not active
			if (Active == false)
				return;

			// Update the elapsed time
			elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

			// If the elapsed time is larger than the frame time
			// we need to switch frames
			if (elapsedTime > frameTime)
			{
				// Move to the next frame
				currentFrame++;

				// If the currentFrame is equal to frameCount reset currentFrame to zero
				if (currentFrame == frameCount)
				{
					currentFrame = 0;
					// If we are not looping deactivate the animation
					if (Looping == false)
						Active = false;
				}

				// Reset the elapsed time to zero
				elapsedTime = 0;
			}

			// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
			sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);

			// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
			destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
			(int)Position.Y - (int)(FrameHeight * scale) / 2,
			(int)(FrameWidth * scale),
			(int)(FrameHeight * scale));
		}


		/// <summary>
		/// Draws the Animation Strip.
		/// </summary>
		/// <param name="spriteBatch">The sprite batch.</param>
		public void Draw(SpriteBatch spriteBatch)
		{
			// Only draw the animation when we are active
			if (Active)
			{
				spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
			}
		}

	}
}
